###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Search.pm.pl -> Search module                                               #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Search;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";

use POSIX;
use strict;

require "Output/Search.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::Search->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(error => $in{'ERROR'});

  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  unless ($SL::QUERY{'Keywords'}) {
    $self->show(DB => $in{'DB'}, ERROR => "MISSING-Keywords");
    return 1;
  }

  $SL::QUERY{'PerPage'} = $GENERAL->{'SEARCH_DEFAULT_PERPAGE'} unless (defined($SL::QUERY{'PerPage'}));
  $SL::QUERY{'Case'}    = $GENERAL->{'SEARCH_DEFAULT_CASE'} unless (defined($SL::QUERY{'Case'}));
  $SL::QUERY{'Whole'}   = $GENERAL->{'SEARCH_DEFAULT_WHOLE'} unless (defined($SL::QUERY{'Whole'}));
  
  my @Fields = $SL::CGI->param('Fields');
     @Fields = @{$GENERAL->{'SEARCH_DEFAULT_FIELDS'}} unless (scalar(@Fields) >= 1);

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  my ($Resources, $ResourcesIndex) = $in{'DB'}->Search(
    TABLE         => "Resources",
    QUERY         => $SL::QUERY{'Keywords'},
    QUERY_COLUMNS => \@Fields,
    QUERY_CASE    => $SL::QUERY{'Case'},
    QUERY_WHOLE   => $SL::QUERY{'Whole'}
  );

  @{$Resources} = grep { $_->{'STATUS'} } @{$Resources};

  my $Pages = &Standard::GetPages($SL::QUERY{'Page'}, scalar(@{$Resources}), $SL::QUERY{'PerPage'});

  @{$Resources} = sort { $b->{'search'}->{'RELEVANCE'} <=> $a->{'search'}->{'RELEVANCE'} || $b->{'search'}->{'COUNT'} <=> $a->{'search'}->{'COUNT'} } @{$Resources};
  @{$Resources} = @{$Resources}[$Pages->{'start'}..$Pages->{'finish'}];

  my @Categories;
  foreach my $resource (@{ $Resources }) {
    push(@Categories, $resource->{'CATEGORY'});
  }
  
  my ($Categories, $CategoriesIndex);
  if (scalar(@Categories) >= 1) {  
    ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
      TABLE   => "Categories",
      WHERE   => {
        ID  => \@Categories
      },
      MATCH   => "ANY"
    );
  }

  my %INPUT;

  $INPUT{'RESOURCES'}     = $Resources;
  $INPUT{'PAGES'}         = $Pages;
  $INPUT{'CATEGORIES'}    = $Categories;
  $INPUT{'CATEGORIES_IX'} = $CategoriesIndex;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::Search->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);

  return 1;
}

1;